package edu.umn.se.trap.logicChecker;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import edu.umn.se.trap.Form;
import edu.umn.se.trap.Transportation;
import edu.umn.se.trap.TravelDetails;
import edu.umn.se.trap.exceptions.TransportationException;

public class CheckSyntaxTransportation implements Observer {

	@Override
	/**
     * Update for checking syntax for transportation
     */
    public void update(Observable o, Object arg) {
        //do a logic, business, or syntax check here using the input form
        Form form = (Form) arg;
        
        Vector<Transportation> transportations = form.getTransportation();
        TravelDetails travelD = form.getTravelDetails();
        LogicChecker l1 = (LogicChecker)o;
        
        int departDate = travelD.getDepartureDate();
        int arrivalDate = travelD.getArrivalDate();
        for (int i = 0; i < transportations.size(); i++)
        {
        	Transportation transport = transportations.elementAt(i);
        	
        	int date = Integer.parseInt(transport.getDate());
        	String type = transport.getType();
        	boolean rental = transport.getRental();
        	String carrier = transport.getCarrier();
        	double miles = transport.getMiles();
        	double amount = transport.getAmount();
        	
        	// Check for date, types, 
        	if (date < departDate || date > arrivalDate)
        	{
        		l1.setException(new TransportationException("Error with Dates in Transportation " + date + " " + departDate + " " + arrivalDate));
        	}
        	if (type.equalsIgnoreCase("air") && carrier.equalsIgnoreCase(""))
        	{
        		l1.setException(new TransportationException("Error, no carrier provided with air type"));
        	}
        	if (type.equalsIgnoreCase("car") && rental && carrier.equalsIgnoreCase("")) 
        	{
        		l1.setException(new TransportationException("Error, no carrier provided with rental car"));
        	}
        	if (type.equalsIgnoreCase("car") && !rental &&  miles <= 0)
        	{
        		l1.setException(new TransportationException("Error, give miles traveled for personal car"));
        	}
        	if (amount < 0)
        	{
        		l1.setException(new TransportationException("Error, amount is not greater than zero " + amount));
        	}
        }
	}
}